package com.wowangz.cms.systemmanage.databaseresource.service;

import java.util.HashMap;
import java.util.List;

import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.systemmanage.databaseresource.pojo.DatabaseBinding;
import com.wowangz.cms.systemmanage.databaseresource.pojo.DatabaseResource;
import com.wowangz.cms.systemmanage.databaseresource.pojo.DatabaseStore;

public class DatabaseBindingService extends BaseService {

	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findDatabaseBindingResultCount(HashMap<String, Object> map) throws ServiceException {
		Long countNum = this.getSqlSession().selectOne("DatabaseBinding.queryDatabaseBindingCount", map);
		return countNum;
	}

	/**
	 * 查询记录集合
	 * 
	 * @param map
	 * @return
	 */
	public List<DatabaseBinding> findDatabaseBindingResultList(HashMap<String, Object> map) throws ServiceException {
		List<DatabaseBinding> list = this.getSqlSession().selectList("DatabaseBinding.queryDatabaseBindingPage", map);
		return list;
	}
	
	public void addDatabaseBinding(String storeid,DatabaseResource databaseresource) throws ServiceException {
		DatabaseStore databaseOrg = new DatabaseStore();
		databaseOrg.setStoreid(Long.valueOf(storeid));
		databaseOrg.setResourceid(databaseresource.getId());
		this.getSqlSession().insert("DatabaseBinding.insertDatabaseStore", databaseOrg);
		
		int bindingcount = this.getSqlSession().update("DatabaseBinding.addDatabaseBinding", databaseresource);
		if(bindingcount != 1)
		{
			throw new ServiceException(MessageResourcesUtils.getMessage(this.getHttpRequest(), "systemmanage.databasebinding.bindingcheck"));
		}
	}
	
	public void updateDatabaseBinding(String storeid,DatabaseResource databaseresource) throws ServiceException {
		DatabaseStore databaseOrg = new DatabaseStore();
		databaseOrg.setStoreid(Long.valueOf(storeid));
		databaseOrg.setResourceid(databaseresource.getId());
		this.getSqlSession().update("DatabaseBinding.updateDatabaseStore", databaseOrg);
		
		int bindingcount = this.getSqlSession().update("DatabaseBinding.addDatabaseBinding", databaseresource);
		if(bindingcount != 1)
		{
			throw new ServiceException(MessageResourcesUtils.getMessage(this.getHttpRequest(), "systemmanage.databasebinding.bindingcheck"));
		}
		
		this.getSqlSession().update("DatabaseBinding.delDatabaseBinding", databaseresource);
	}
	
	public DatabaseResource queryDatabaseBinding(HashMap<String,Object> map){
		DatabaseResource databaseResource = this.getSqlSession().selectOne("DatabaseBinding.queryDatabaseBinding", map);
		return databaseResource;
	}
	
	public List<DatabaseBinding> getBindingListByStoreid(Long storeid){
		return this.getSqlSession().selectList("DatabaseBinding.getBindingListByStoreid", storeid);
	}
	
	public DatabaseResource nextBindingResource(DatabaseResource resource){
		List<DatabaseResource> list = this.getSqlSession().selectList("DatabaseBinding.nextBindingResource", resource);
		return list!=null?list.get(0):null;
	}
}
